package gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileWriter;
import java.util.Random;

public class P4 extends JFrame {
    private static P4 p4;

    private JMenuBar jMenuBar = new JMenuBar();

    private JMenu fileMenu = new JMenu("File");
    private JMenuItem newMenuItem = new JMenuItem("New");
    private JMenuItem getMenuItem = new JMenuItem("Get7Num");
    private JMenuItem saveMenuItem = new JMenuItem("Save");
    private JMenuItem exitMenuItem = new JMenuItem("Exit");

    private JTextArea jTextArea = new JTextArea(20, 20);
    private JScrollPane jScrollPane = new JScrollPane(jTextArea);


    public P4() {
        setTitle("第4题");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(600, 400);

        this.init();

        this.setJMenuBar(jMenuBar);
        this.add(jScrollPane);

        this.setVisible(true);
    }

    public void init() {
        // 绑定快捷键盘
        KeyStroke ctrln = KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK);
        newMenuItem.setAccelerator(ctrln);

        KeyStroke ctrlg = KeyStroke.getKeyStroke(KeyEvent.VK_G, InputEvent.CTRL_MASK);
        getMenuItem.setAccelerator(ctrlg);

        KeyStroke ctrls = KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK);
        saveMenuItem.setAccelerator(ctrls);

        KeyStroke ctrle = KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.CTRL_MASK);
        exitMenuItem.setAccelerator(ctrle);

        // 绑定事件
        newMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jTextArea.setText("");
            }
        });

        // 生成随机数
        getMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Random random = new Random();
                for (int i = 0; i < 7; i++) {
                    int num = random.nextInt(15) + 1;
                    jTextArea.append(num + " ");
                }
            }
        });

        // 保存文件对话框
        saveMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();

                int option = fileChooser.showSaveDialog(p4);

                if(option == JFileChooser.APPROVE_OPTION){
                    File file = fileChooser.getSelectedFile();
                    try {
                        FileWriter fileWriter = new FileWriter(file);
                        fileWriter.write(jTextArea.getText());
                        fileWriter.close();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });


        exitMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        fileMenu.add(newMenuItem);
        fileMenu.add(getMenuItem);
        fileMenu.add(saveMenuItem);
        fileMenu.add(exitMenuItem);

        jMenuBar.add(fileMenu);

        jTextArea.setFont(new Font("楷体", Font.PLAIN, 20));
    }

    public static void main(String[] args) {
        p4 = new P4();
    }

}
